
<!DOCTYPE HTML>
<html lang="" >
    <head>
        <meta charset="UTF-8">
        <meta content="text/html; charset=utf-8" http-equiv="Content-Type">
        <title>emit · GitBook</title>
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <meta name="description" content="">
        <meta name="generator" content="GitBook 3.2.2">
        
        
        
    
    <link rel="stylesheet" href="gitbook/style.css">

    
            
                
                <link rel="stylesheet" href="gitbook/gitbook-plugin-highlight/website.css">
                
            
                
                <link rel="stylesheet" href="gitbook/gitbook-plugin-search/search.css">
                
            
                
                <link rel="stylesheet" href="gitbook/gitbook-plugin-fontsettings/website.css">
                
            
        

    

    
        
    
        
    
        
    
        
    
        
    
        
    

        
    
    
    <meta name="HandheldFriendly" content="true"/>
    <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <link rel="apple-touch-icon-precomposed" sizes="152x152" href="gitbook/images/apple-touch-icon-precomposed-152.png">
    <link rel="shortcut icon" href="gitbook/images/favicon.ico" type="image/x-icon">

    
    <link rel="next" href="socket.io-client-api.html" />
    
    
    <link rel="prev" href="socket.io-api.html" />
    

    </head>
    <body>
        
<div class="book">
    <div class="book-summary">
        
            
<div id="book-search-input" role="search">
    <input type="text" placeholder="Type to search" />
</div>

            
                <nav role="navigation">
                


<ul class="summary">
    
    

    

    
        
        
    
        <li class="chapter " data-level="1.1" data-path="./">
            
                <a href="./">
            
                    
                    Introduction
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2" data-path="socket.io-api.html">
            
                <a href="socket.io-api.html">
            
                    
                    socket.io API
            
                </a>
            

            
        </li>
    
        <li class="chapter active" data-level="1.3" data-path="emit.html">
            
                <a href="emit.html">
            
                    
                    emit
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4" data-path="socket.io-client-api.html">
            
                <a href="socket.io-client-api.html">
            
                    
                    socket.io-client API
            
                </a>
            

            
        </li>
    

    

    <li class="divider"></li>

    <li>
        <a href="https://www.gitbook.com" target="blank" class="gitbook-link">
            Published with GitBook
        </a>
    </li>
</ul>


                </nav>
            
        
    </div>

    <div class="book-body">
        
            <div class="body-inner">
                
                    

<div class="book-header" role="navigation">
    

    <!-- Title -->
    <h1>
        <i class="fa fa-circle-o-notch fa-spin"></i>
        <a href="." >emit</a>
    </h1>
</div>




                    <div class="page-wrapper" tabindex="-1" role="main">
                        <div class="page-inner">
                            
<div id="book-search-results">
    <div class="search-noresults">
    
                                <section class="normal markdown-section">
                                
                                <h2 id="emit-cheatsheet">Emit cheatsheet</h2>
<pre><code class="lang-js">
io.on(<span class="hljs-string">&apos;connect&apos;</span>, onConnect);

<span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">onConnect</span>(<span class="hljs-params">socket</span>)</span>{

  <span class="hljs-comment">// &#x53D1;&#x9001;&#x7ED9;&#x5BA2;&#x6237;&#x7AEF;</span>
  socket.emit(<span class="hljs-string">&apos;hello&apos;</span>, <span class="hljs-string">&apos;can you hear me?&apos;</span>, <span class="hljs-number">1</span>, <span class="hljs-number">2</span>, <span class="hljs-string">&apos;abc&apos;</span>);

  <span class="hljs-comment">// &#x53D1;&#x9001;&#x7ED9;&#x9664;&#x53D1;&#x9001;&#x65B9;&#x4E4B;&#x5916;&#x7684;&#x6240;&#x6709;&#x5BA2;&#x6237;&#x7AEF;</span>
  socket.broadcast.emit(<span class="hljs-string">&apos;broadcast&apos;</span>, <span class="hljs-string">&apos;hello friends!&apos;</span>);

  <span class="hljs-comment">// &#x9664;&#x4E86;&#x53D1;&#x9001;&#x8005;&#x4E4B;&#x5916;&#xFF0C;&#x53D1;&#x9001;&#x5230;&#x201C;&#x6E38;&#x620F;&#x201D;&#x5BA4;&#x4E2D;&#x7684;&#x6240;&#x6709;&#x5BA2;&#x6237;&#x7AEF;</span>
  socket.to(<span class="hljs-string">&apos;game&apos;</span>).emit(<span class="hljs-string">&apos;nice game&apos;</span>, <span class="hljs-string">&quot;let&apos;s play a game&quot;</span>);

  <span class="hljs-comment">// &#x53D1;&#x9001;&#x5230;&apos;game1&apos;&#x548C;/&#x6216;&apos;game2&apos;&#x623F;&#x95F4;&#x4E2D;&#x7684;&#x6240;&#x6709;&#x5BA2;&#x6237;&#x7AEF;&#xFF0C;&#x53D1;&#x9001;&#x8005;&#x9664;&#x5916;</span>
  socket.to(<span class="hljs-string">&apos;game1&apos;</span>).to(<span class="hljs-string">&apos;game2&apos;</span>).emit(<span class="hljs-string">&apos;nice game&apos;</span>, <span class="hljs-string">&quot;let&apos;s play a game (too)&quot;</span>);

  <span class="hljs-comment">// &#x53D1;&#x9001;&#x5230;&#x201C;&#x6E38;&#x620F;&#x201D;&#x5BA4;&#x4E2D;&#x7684;&#x6240;&#x6709;&#x5BA2;&#x6237;&#xFF0C;&#x5305;&#x62EC;&#x53D1;&#x4EF6;&#x4EBA;</span>
  io.in(<span class="hljs-string">&apos;game&apos;</span>).emit(<span class="hljs-string">&apos;big-announcement&apos;</span>, <span class="hljs-string">&apos;the game will start soon&apos;</span>);

  <span class="hljs-comment">// &#x53D1;&#x9001;&#x5230;&#x547D;&#x540D;&#x7A7A;&#x95F4;&apos;myNamespace&apos;&#x4E2D;&#x7684;&#x6240;&#x6709;&#x5BA2;&#x6237;&#x7AEF;&#xFF0C;&#x5305;&#x62EC;&#x53D1;&#x9001;&#x8005;</span>
  io.of(<span class="hljs-string">&apos;myNamespace&apos;</span>).emit(<span class="hljs-string">&apos;bigger-announcement&apos;</span>, <span class="hljs-string">&apos;the tournament will start soon&apos;</span>);

  <span class="hljs-comment">// &#x53D1;&#x9001;&#x5230;&#x5355;&#x72EC;&#x7684;socketid&#xFF08;&#x79C1;&#x4EBA;&#x6D88;&#x606F;&#xFF09;</span>
  socket.to(<span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">socketid</span>&gt;</span>).emit(&apos;hey&apos;, &apos;I just met you&apos;);

  // &#x53D1;&#x9001;&#x4E0E;&#x786E;&#x8BA4;
  socket.emit(&apos;question&apos;, &apos;do you think so?&apos;, function (answer) {});

  // &#x53D1;&#x9001;&#x65E0;&#x538B;&#x7F29;
  socket.compress(false).emit(&apos;uncompressed&apos;, &quot;that&apos;s rough&quot;);

  // &#x53D1;&#x9001;&#x53EF;&#x80FD;&#x5728;&#x5BA2;&#x6237;&#x7AEF;&#x672A;&#x51C6;&#x5907;&#x597D;&#x63A5;&#x6536;&#x6D88;&#x606F;&#x65F6;&#x4E22;&#x5F03;&#x7684;&#x6D88;&#x606F;
  socket.volatile.emit(&apos;maybe&apos;, &apos;do you really need it?&apos;);

  // &#x53D1;&#x9001;&#x5230;&#x6B64;&#x8282;&#x70B9;&#x4E0A;&#x7684;&#x6240;&#x6709;&#x5BA2;&#x6237;&#x7AEF;&#xFF08;&#x4F7F;&#x7528;&#x591A;&#x4E2A;&#x8282;&#x70B9;&#x65F6;&#xFF09;
  io.local.emit(&apos;hi&apos;, &apos;my lovely babies&apos;);

};
</span></code></pre>
<p><strong>Note:</strong> &#x4EE5;&#x4E0B;&#x4E8B;&#x4EF6;&#x662F;&#x5E94;&#x7528;&#x4FDD;&#x7559;&#x7684;</p>
<ul>
<li><code>error</code></li>
<li><code>connect</code></li>
<li><code>disconnect</code></li>
<li><code>disconnecting</code></li>
<li><code>newListener</code></li>
<li><code>removeListener</code></li>
<li><code>ping</code></li>
<li><code>pong</code></li>
</ul>

                                
                                </section>
                            
    </div>
    <div class="search-results">
        <div class="has-results">
            
            <h1 class="search-results-title"><span class='search-results-count'></span> results matching "<span class='search-query'></span>"</h1>
            <ul class="search-results-list"></ul>
            
        </div>
        <div class="no-results">
            
            <h1 class="search-results-title">No results matching "<span class='search-query'></span>"</h1>
            
        </div>
    </div>
</div>

                        </div>
                    </div>
                
            </div>

            
                
                <a href="socket.io-api.html" class="navigation navigation-prev " aria-label="Previous page: socket.io API">
                    <i class="fa fa-angle-left"></i>
                </a>
                
                
                <a href="socket.io-client-api.html" class="navigation navigation-next " aria-label="Next page: socket.io-client API">
                    <i class="fa fa-angle-right"></i>
                </a>
                
            
        
    </div>

    <script>
        var gitbook = gitbook || [];
        gitbook.push(function() {
            gitbook.page.hasChanged({"page":{"title":"emit","level":"1.3","depth":1,"next":{"title":"socket.io-client API","level":"1.4","depth":1,"path":"socket.io-client-api.md","ref":"socket.io-client-api.md","articles":[]},"previous":{"title":"socket.io API","level":"1.2","depth":1,"path":"socket.io-api.md","ref":"socket.io-api.md","articles":[]},"dir":"ltr"},"config":{"gitbook":"*","theme":"default","variables":{},"plugins":[],"pluginsConfig":{"highlight":{},"search":{},"lunr":{"maxIndexSize":1000000,"ignoreSpecialCharacters":false},"sharing":{"facebook":true,"twitter":true,"google":false,"weibo":false,"instapaper":false,"vk":false,"all":["facebook","google","twitter","weibo","instapaper"]},"fontsettings":{"theme":"white","family":"sans","size":2},"theme-default":{"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"},"showLevel":false}},"structure":{"langs":"LANGS.md","readme":"README.md","glossary":"GLOSSARY.md","summary":"SUMMARY.md"},"pdf":{"pageNumbers":true,"fontSize":12,"fontFamily":"Arial","paperSize":"a4","chapterMark":"pagebreak","pageBreaksBefore":"/","margin":{"right":62,"left":62,"top":56,"bottom":56}},"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"}},"file":{"path":"emit.md","mtime":"2017-03-06T14:57:34.982Z","type":"markdown"},"gitbook":{"version":"3.2.2","time":"2017-03-07T13:45:59.536Z"},"basePath":".","book":{"language":""}});
        });
    </script>
</div>

        
    <script src="gitbook/gitbook.js"></script>
    <script src="gitbook/theme.js"></script>
    
        
        <script src="gitbook/gitbook-plugin-search/search-engine.js"></script>
        
    
        
        <script src="gitbook/gitbook-plugin-search/search.js"></script>
        
    
        
        <script src="gitbook/gitbook-plugin-lunr/lunr.min.js"></script>
        
    
        
        <script src="gitbook/gitbook-plugin-lunr/search-lunr.js"></script>
        
    
        
        <script src="gitbook/gitbook-plugin-sharing/buttons.js"></script>
        
    
        
        <script src="gitbook/gitbook-plugin-fontsettings/fontsettings.js"></script>
        
    

    </body>
</html>

